package com.linkhub.oms.admin.modules.user.attachment.service.impl;
import java.time.LocalDateTime;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.user.attachment.domain.vo.UserAttachmentVO;
import com.linkhub.oms.admin.modules.user.attachment.service.IUserAttachmentService;
import com.linkhub.oms.common.enums.mp.DataStatusEnum;
import com.linkhub.oms.common.enums.mp.UserAttachmentEnum;
import com.linkhub.oms.dao.mapper.UserAttachmentEntityMapper;
import com.linkhub.oms.entity.UserAttachmentEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户附件表 服务实现类
 * </p>
 *
 * @author Frank
 * @since 2024-05-26
 */
@Service
public class UserAttachmentServiceImpl extends ServiceImpl<UserAttachmentEntityMapper, UserAttachmentEntity> implements IUserAttachmentService {

    @Override
    public List<UserAttachmentEntity> getByUserId(String userId, Long bizId, UserAttachmentEnum userAttachmentEnum) {
        return lambdaQuery()
                .eq(UserAttachmentEntity::getUserId, userId)
                .eq(UserAttachmentEntity::getBizId, bizId)
                .eq(UserAttachmentEntity::getBizType, userAttachmentEnum)
                .eq(UserAttachmentEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttachmentBatch(String userId, Long bizId, UserAttachmentEnum bizType, List<UserAttachmentVO> attachmentList) {
        if (CollUtil.isEmpty(attachmentList)) {
            return;
        }
        List<UserAttachmentEntity> entities = Lists.newArrayList();
        attachmentList.forEach(attachment -> {
            UserAttachmentEntity userAttachmentEntity = new UserAttachmentEntity();
            userAttachmentEntity.setUserId(userId);
            userAttachmentEntity.setBizId(bizId);
            userAttachmentEntity.setBizType(bizType);
            userAttachmentEntity.setFileName(attachment.getFileName());
            userAttachmentEntity.setFileUrl(attachment.getFileUrl());
            entities.add(userAttachmentEntity);
        });
        this.saveBatch(entities);
    }
}
